package com.course.db;

import java.sql.ResultSet;

public class Teacher extends ExecuteDB {

	//members
	private int Id;
	private String name;
	private int user_id;
	private String introduction;
	private String strSql;
	
	public Teacher()
	{
		this.Id = 0;
		this.name = "";
		this.introduction = "";
		this.user_id = 0;
	}
	
	public String CountSql()
	{
		this.strSql = "select count(*) from teacher";
		return this.strSql;
	} 
	
	//add a teacher
	public boolean add()
	{

		this.strSql="insert into teacher ";
		this.strSql=this.strSql + "(name,user_id,introduction) ";
		this.strSql=this.strSql + "values('" + this.name + "','" + this.user_id+ "','" + this.introduction + "')";
		System.out.println(this.strSql);
		boolean isAdd = super.exeSql(this.strSql);
        return isAdd;
	}
	
	public int lastid()
	{
		int lid = 0;
		this.strSql = "select last_insert_id() as id from teacher limit 1";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
			if (rs.next())
				lid = rs.getInt("id");
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return lid;
	}
	
	//delete a teacher by id
    public boolean delete(int TeacherID)
    {
       this.strSql = "delete from teacher where Id in (";
       this.strSql = this.strSql + TeacherID +")";

       boolean isDelete = super.exeSql(this.strSql);
       return isDelete;
    }
    
  //initialize teacher information
 	public boolean init()
	{
		this.strSql="select * from `teacher` where Id=";
		this.strSql=this.strSql + "'" + this.Id + "'";

		try
		{
			ResultSet rs = super.exeQuery(this.strSql);
			if (rs.next())
			{
				this.Id=rs.getInt("Id");
				this.name=rs.getString("name");
				this.user_id=rs.getInt("user_id");
				this.introduction=rs.getString("introduction");
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			return false;
		}
	}
 	
 	//modify teacher infomation
    public boolean modify_teacher()
	{
		this.strSql="update user teacher";
		this.strSql=this.strSql + " name=" + "'" + this.name + "',";
		this.strSql=this.strSql + " user_id=" + this.user_id + ",";
		this.strSql=this.strSql + " introduction=" + this.introduction;
		this.strSql=this.strSql + " where Id=" + this.Id;
		
		boolean isUpdate = super.exeSql(this.strSql);

		return isUpdate;
	}
    
  //show teachers
    public ResultSet show_teachers()
	{
		this.strSql="select * from `teacher`";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return rs;
	}
    
    public int teacher_id( )
    {
    	int teacher_id = 0;
    	this.strSql = "select Id from `teacher` where user_id = " + this.user_id;
		ResultSet rs = null;
    	try
		{
			rs = super.exeQuery(this.strSql);
			
			if(rs.first())
				teacher_id = rs.getInt("Id");
			else
				teacher_id = 0;
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
    	
    	return teacher_id;
    }
    
    public ResultSet show_courses()
    {
    	this.strSql="select * from `course_basic` where Id in (select course_id from `open_course` where teacher_id = "+ this.Id +" )";
		ResultSet rs = null;
		
		try
		{
			rs = super.exeQuery(this.strSql);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		
		return rs;
    }

	public int getId() {
		return Id;
	}

	public void setId(int id) {
		Id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	public String getIntroduction() {
		return introduction;
	}

	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}

	public String getStrSql() {
		return strSql;
	}

	public void setStrSql(String strSql) {
		this.strSql = strSql;
	}

	
}
